<template>
  <div class="container">
    <div class="login-container">
      <div class="left-container">
        <div class="title">
          <span>登录</span>
        </div>
        <div class="input-container">
          <el-form :model="loginForm" :rules="rules" ref="loginForms">
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
            </el-form-item>

            <el-form-item prop="password">
              <el-input v-model="loginForm.password" placeholder="请输入密码"></el-input>
            </el-form-item>
          </el-form>


        </div>
      </div>
      <div class="right-container">
        <div class="regist-container">
          <span class="regist" @click="register">注册</span>
        </div>
        <div class="action-container">
          <span @click="login">登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import store from "@/store";
export default {
  data() {
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入密码"))
      } else if (value.length < 6) {
        callback(new Error("密码不能少于六位"))
      } else {
        callback();
      }
    }
    return {
      loginForm: [],
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'change'}
        ],
        password: [
          // {required: true, message: '请输入密码', trigger: 'blur'},
          // {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'change'}
          {validator: validatePassword, trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    async login() {
      await this.$refs.loginForms.validate();
      var params = new URLSearchParams();
      params.append('username', this.loginForm.username);
      params.append('password', this.loginForm.password);
      request.post("user/login", params)
          .then(res => {
            if (res.data.code == 1) {
              // 存储用户信息
              store.commit("app/SET_CURRENT_USER", {
                userId: res.data.data.id,
                token: res.data.data.token,
                username: res.data.data.username,
                permissions: res.data.data.permission
              })
              this.$router.push('/home')
            } else {
              this.$message({
                message: '用户名或密码错误',
                type: 'error'
              })
            }
          })
          .catch(err => {
            this.$message({
              message: '当前账号已经被登录',
              type: 'error'
            })
          })
    },
    register() {
      this.$router.push('/register')
    }
  }
}

</script>

<style scoped>
/deep/ .el-container {
  margin: 0px;
}

* {
  padding: 0;
  margin: 0;
}

html {
  height: 100%;
  overflow: hidden;
}

body {
  height: 100%;
  overflow: hidden;
  background-image: linear-gradient(to bottom right, rgb(114, 135, 254), rgb(130, 88, 186));
}

.container {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background-image: linear-gradient(to right, green, goldenrod, green);
}

.login-container {
  width: 600px;
  height: 300px;
  margin: 0 auto;
  margin-top: 27vh;
  border-radius: 15px;
  box-shadow: 0 10px 50px 0px rgb(59, 45, 159);
  background-color: rgb(95, 76, 194);

}

.left-container {
  display: inline-block;
  width: 330px;
  border-top-left-radius: 15px;
  border-bottom-left-radius: 15px;
  padding: 60px;
  background-image: linear-gradient(to bottom right, rgb(105, 163, 76), rgb(92, 103, 211));
}

.title {
  color: #fff;
  font-size: 18px;
  font-weight: 200;
}

.title span {
  border-bottom: 3px solid rgb(237, 221, 22);
}

.input-container {
  padding: 20px 0;
}

input {
  border: 0;
  background: none;
  outline: none;
  color: #fff;
  margin: 20px 0;
  display: block;
  width: 100%;
  padding: 5px 0;
  transition: .2s;
  border-bottom: 1px solid rgb(199, 191, 219);
}

input:hover {
  border-bottom-color: #fff;
}

::-webkit-input-placeholder {
  color: rgb(199, 191, 219);
}

.right-container {
  position: relative;
  width: 145px;
  display: inline-block;
  height: calc(100% - 120px);
  vertical-align: top;
  padding: 60px 0;
}

.regist-container {
  text-align: center;
  color: #fff;
  font-size: 18px;
  font-weight: 200;
}

.regist-container span {
  cursor: pointer;
}

.action-container {
  font-size: 10px;
  color: #fff;
  text-align: center;
  position: relative;
  top: 200px;
}

.action-container span {
  border: 1px solid rgb(237, 221, 22);
  padding: 10px;
  display: inline;
  line-height: 20px;
  border-radius: 20px;
  position: absolute;
  bottom: 10px;
  left: calc(72px - 20px);
  transition: .2s;
  cursor: pointer;
}

.action-container span:hover {
  background-color: rgb(237, 221, 22);
  color: rgb(95, 76, 194);
}
</style>
